package com.icepeach.icepeachhomeback.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.icepeach.icepeachhomeback.entity.Relation;
import com.icepeach.icepeachhomeback.service.RelationService;
import com.icepeach.icepeachhomeback.mapper.RelationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 24273
* @description 针对表【relation】的数据库操作Service实现
* @createDate 2023-05-03 21:48:38
*/
@Service
public class RelationServiceImpl extends ServiceImpl<RelationMapper, Relation> implements RelationService{

    @Autowired
    private RelationMapper relationMapper;

    @Override
    public void batchInsertRelationByBusinessId(Integer businessType, Integer targetType, String businessId, String businessName,List<String> targetIdList) {
        List<Relation> relations = buildRelationByBusinessId(businessType, targetType, businessId, businessName, targetIdList);
        relationMapper.batchInsertRelationByBusinessId(relations);
    }

    @Override
    public void batchInsertRelationByTargetId(Integer businessType, Integer targetType, String targetId,String targetName, List<String> businessIdList) {
        List<Relation> relations = buildRelationByTargetId(businessType, targetType, targetId, targetName, businessIdList);
        relationMapper.batchInsertRelationByBusinessId(relations);
    }

    @Override
    public List<Relation> buildRelationByBusinessId(Integer businessType, Integer targetType, String businessId,String businessName, List<String> targetIdList) {
        ArrayList<Relation> list = new ArrayList<>();
        if (CollectionUtil.isEmpty(targetIdList) || null == businessId){
            return list;
        }
        targetIdList.forEach(item -> {
            Relation relation = Relation.builder()
                    .businessId(businessId)
                    .businessName(businessName)
                    .businessType(businessType)
                    .targetId(item)
                    .targetType(targetType)
                    .build();
            list.add(relation);
        });
        return list;
    }

    @Override
    public List<Relation> buildRelationByTargetId(Integer businessType, Integer targetType, String targetId,String targetName, List<String> businessIdList) {
        ArrayList<Relation> list = new ArrayList<>();
        if (CollectionUtil.isEmpty(businessIdList) || null == targetId){
            return list;
        }
        businessIdList.forEach(item -> {
            Relation relation = Relation.builder()
                    .businessId(item)
                    .businessType(businessType)
                    .targetId(item)
                    .targetName(targetName)
                    .targetType(targetType)
                    .build();
            list.add(relation);
        });
        return list;
    }
}
